package com.itestor.iptv.modules.ne.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.itestor.iptv.modules.ne.dao.IptvLostRateDao;
import com.itestor.iptv.modules.ne.domain.IptvLostRate;
import com.itestor.iptv.modules.ne.domain.RobotManage;
import com.itestor.iptv.modules.ne.service.IptvLostRateService;
import com.itestor.iptv.modules.ne.vo.IptvLostRateBean;
import com.itestor.iptv.modules.ne.vo.PictureData;
import com.itestor.iptv.utils.JdbcUtil;
/**
 * 丢包率service
 * @author masaki
 */
public class IptvLostRateServiceImpl implements IptvLostRateService{
    
    private static final Logger logger=Logger.getLogger(IptvLostRateServiceImpl.class);
    
	/**
	 * iptvLostRateDao
	 */
	private IptvLostRateDao iptvLostRateDao;
	/**
	 * @param iptvLostRateDao
	 */
	public void setIptvLostRateDao(IptvLostRateDao iptvLostRateDao) {
		this.iptvLostRateDao = iptvLostRateDao;
	}
	/**
	 * 获得所有的丢包率
	 */
	@SuppressWarnings("unchecked")
	public List<IptvLostRate> getAllIptvLostRate() {
		Session session = iptvLostRateDao.getHibernateSession();
		return session.createQuery("from IptvLostRate").list();
	}
	/**
	 * 通过ids获得所需要的对象，绘图用
	 */
	@SuppressWarnings("unchecked")
	public List<Object[]> getIptvLostRateByIds(Long[] ids) {
		Session session = iptvLostRateDao.getHibernateSession();
		List list = session.createQuery("select ilr.idRobot,rm.name,ilr.errorCount,ilr.tatolCount,ilr.logTime from IptvLostRate ilr,RobotManage rm where ilr.idRobot = rm.idRobot and ilr.idRobot in(:ids) order by ilr.idRobot , ilr.logTime").setParameterList("ids", ids).list();
		return list;
	}
	
	public List<Object[]> getIptvLostRateByIdsAndTime(Long ids[],Long startTime,Long endTime){
		Session session = iptvLostRateDao.getHibernateSession();
		List list = session.createQuery("select ilr.idRobot,rm.name,ilr.errorCount,ilr.tatolCount,ilr.logTime from IptvLostRate ilr,RobotManage rm where ilr.idRobot = rm.idRobot and ilr.idRobot in(:ids) and ilr.logTime >= :startTime and ilr.logTime <= :endTime order by ilr.idRobot , ilr.logTime").setParameterList("ids", ids).setParameter("startTime", startTime).setParameter("endTime", endTime).list();
		return list;
	}
	
	/**
	 * 
	 * @param ids
	 * @param startTime
	 * @param endTime
	 * @return List
	 */
	public List<IptvLostRateBean> getIptvLostRateBeanByIdsAndTime(Integer ids[],Long startTime,Long endTime,Integer statunitsec){	    
	    List<IptvLostRateBean>  beanList =new ArrayList<IptvLostRateBean>();	    
        Session session = iptvLostRateDao.getHibernateSession();
        List<RobotManage> list = session.createQuery("from RobotManage rm where rm.idRobot in(:ids)").setParameterList("ids", ids).list();
        
        for(RobotManage vo : list){
            IptvLostRateBean bean =new IptvLostRateBean();  
            List<PictureData> dataList= getIptvLostRateByIdAndTime(vo.getIdRobot(),startTime,endTime,statunitsec);
            bean.setDataList(dataList);
            bean.setIdRobot(vo.getIdRobot());
            bean.setName(vo.getName());
            beanList.add(bean);
        }
        
        return beanList;
    }
    
	public List<PictureData> getIptvLostRateByIdAndTime(Integer id,Long startTime,Long endTime,Integer statunitsec){
	    SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.US);
	    
		StringBuffer sql = new StringBuffer();
		sql.append("{call pr_iptv_histroylostrate(?,?,?,?)}");
		Session session = iptvLostRateDao.getHibernateSession();
		Connection conn = session.connection();
		CallableStatement callstmt = null;
		ResultSet rs = null;
		
		List<PictureData> pictureDataList = new ArrayList<PictureData>();
		try{
			callstmt = conn.prepareCall(sql.toString());
			callstmt.setInt(1, id.intValue());
			String start_time =sdf.format(new Date(startTime*1000L));
			String end_time =sdf.format(new Date(endTime*1000L));

			
			
            callstmt.setString(2, start_time);
            callstmt.setString(3, end_time);            
            callstmt.setInt(4, statunitsec);
            
            logger.info("getIptvLostRateByIdAndTime   id :" +id +" start_time :" +start_time +"end_time " +end_time +" statunitsec" +statunitsec);
			
            rs = callstmt.executeQuery();
			while(rs.next()){
				PictureData pictureData = new PictureData();
				pictureData.setRate(rs.getDouble("LostRate"));
				pictureData.setTime(rs.getString("LogTime"));	
				
				System.out.println("lostrate :"+rs.getDouble("LostRate") +" date : " + rs.getString("LogTime"));
				pictureDataList.add(pictureData);
			}
		} catch (SQLException e) {           
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,callstmt, conn);
		}
		return pictureDataList;
	}
}
